package com.collins.thread.practices.ticket;

import java.util.concurrent.locks.ReentrantLock;

public class TicketLockRunnable implements Runnable {
    private static int ticket = 0;
    private static ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {

        while (true) {
            lock.lock();
            try {
                if (ticket == 1000) {
                    break;
                } else {
                    Thread.sleep(50);
                    System.out.println(Thread.currentThread().getName() + " sells No." + ticket + " ticket");
                    ticket++;
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }
}
